var myDatetimepicker = myApp.directive('myDatetimepicker', ['$rootScope', '$filter', function ($rootScope, $filter) {
    return {
        restrict: 'E',
        require: '?ngModel', scope: true,
        link: function ($scope, elem, attrs, ctrl) {
            if (attrs.minDate) {
                // $(elem).find('.input-group').attr('data-date-start-date', attrs.minDate);
                $(elem).find('.myDate').attr('data-date-start-date', attrs.minDate);
            }
            if (attrs.maxDate) {
                // $(elem).find('.input-group').attr('data-date-end-date', attrs.maxDate);
                $(elem).find('.myDate').attr('data-date-end-date', attrs.maxDate); 
            }
            if (attrs.name) {
                $(elem).find('input').attr('name', attrs.name);
                if (attrs.required) {
                    $(elem).find('input').attr('required', true);
                }
                if (attrs.csValidate) {
                    $(elem).find('input').attr('cs-validate', attrs.csValidate);
                }
                if (attrs.csValidates) {
                    $(elem).find('input').attr('cs-validates', attrs.csValidates);
                }
            }

            // $(elem).find('.input-group').datetimepicker({
            $(elem).find('.myDate').datetimepicker({
                language: 'zh-CN',
                autoclose: true,
                // isRTL: App.isRTL(),
                format: "yyyy-mm-dd hh:ii",
                pickerPosition: "bottom-left"
                // pickerPosition: (App.isRTL() ? "bottom-right" : "bottom-left")
            });

            $scope.clear = function () {
                ctrl.$setViewValue(null);
                if (attrs.ngChange) {
                    $parse(attrs.ngChange)($scope);
                }
            }

            $scope.dateModel = function () {
                if (arguments && arguments.length > 0) {
                    ctrl.$setViewValue(new Date(arguments[0]));
                } else {
                    if (ctrl.$viewValue) {
                        if (!(ctrl.$viewValue instanceof Date)) {
                            ctrl.$setViewValue(new Date(ctrl.$viewValue));
                        }
                        return $filter('date')(ctrl.$viewValue, 'yyyy-MM-dd HH:mm');
                    }
                }
            };

            $scope.triggle = function(evnet) {
                $(elem).find('.myDate').datetimepicker('show');
            }
        },
        template: '<div class="input-group date form_datetime">'
            + '<input type="text" ng-model="dateModel" ng-model-options="{getterSetter: true }" readonly class="form-control myDate">'
            + '<span class="input-group-btn">'
            + ' <button class="btn default" type="button" ng-click="clear()"><i class="fa fa-times"></i></button>'
            + '<button class="btn default date-set" type="button" ng-click="triggle()"><i class="fa fa-calendar"></i></button>'
            + '</span>'
        + '</div>'
    };
}]);